package com.agan.aganjavaspyder.crawler.ct_test.beijing;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.pipeline.JsonPipeline;
import com.geccocrawler.gecco.request.HttpPostRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static cn.hutool.core.date.DatePattern.PURE_DATETIME_PATTERN;

/**
 * @author agan
 * @ClassName HrefPipeline
 * @Description
 * @Date 2020/10/14 5:28 下午
 **/
@PipelineName("BjHrefPipeline")
@Slf4j
@Service
public class BjHrefPipeline extends JsonPipeline{


    @Override
    public void process(JSONObject jo) {

        System.out.println("json:" + JSONUtil.toJsonPrettyStr(jo));


    }

    public String setRequestParam(String url, Map<String, String> params) {
        StringBuffer result = new StringBuffer(url);

        if(params.size() > 0){
            result.append("?");

            for(Map.Entry<String, String> cur : params.entrySet()){
                result.append(cur.getKey()).append("=").append(cur.getValue()).append("&");
            }

            result.deleteCharAt(result.length()-1);
        }

        return result.toString();
    }


    public static void main(String[] args) {


        String url = "https://z.bj.189.cn/itemRest/commodity/list";
        HttpPostRequest req = new HttpPostRequest();
        req.addHeader("Accept","*/*");
        req.addHeader("Accept-Encoding","gzip, deflate, br");
        req.addHeader("Accept-Language","zh-CN,zh;q=0.9,en;q=0.8");
        req.addHeader("Cache-Control","no-cache");
        req.addHeader("Connection","keep-alive");
        req.addHeader("Cookie","svid=8FB79C4D0A124CA0B8C3F14027F811FD; s_fid=3A38D3A306DCDDE1-29D62542E4EAD012; lvid=1b5cd9f92123147c082c73d33d6c2dbf; nvid=1; trkId=E86703CE-1537-4D3C-B1D3-2D935E8C01D1; 0yHmMQUi4f9KS=52q1wG31MPE1o6_yib0Ufq.zubGTIP_sPufimk0buk17G_KX0CQOaqJWmJVxooG.7fb9Qdt.qk3TDVl8MbidV.a; WT_FPC=id=120135cfaf73cf2745a1602639740969; oldtime=18550; WT_SS=16026592176525981dbb9a; s_cc=true; jsession_id_scsva=n17A0D36222AD98242AFB352510A20EFA-1; loginStatus=non-logined; Hm_lvt_79fae2027f43ca31186e567c6c8fe33e=1602663329; Hm_lpvt_79fae2027f43ca31186e567c6c8fe33e=1602663329; WT_si_n=MM_ADV_HOME; channel=wt-vivoz6; trkHmCitycode=0; trkHmCoords=0; trkHmPageName=0; 0yHmMQUi4f9KO=5cyfRja4twdsDBkWaz94MGZKcxRdVoohCHbRvPSPXxYP0LpvWvM74Jm8tqbGrSYQjCsdjiUTNkBhyIEa9bvfMLa; 5224c5f07e5da28b61=376d8e5131078e7c1bb22b85719290e6; Hm_lvt_9004434f3a31031836fd6a07df4d1ae8=1602663447,1602669636,1602669689,1602669694; cityCode=bj; trkHmClickCoords=306%2C120%2C4528; s_sq=eship-189-js%3D%2526pid%253D189.cn%2526pidt%253D1%2526oid%253Dfunctiononclick%252528event%252529%25257BredirectProvince%252528%252527bj%252527%252529%25253B%25257D%2526oidt%253D2%2526ot%253DA; SHOPID_COOKIEID=10001; 0yHmMQUi4f9KT=5UgFndY5Dzr3qqqmTxNyPXakHvW2E4GDQoS_zub_8qkhHpPRKc99o9xHG_6a_Txid5KEywPKhMTVwK69CElpEU0ST9FbMc0q.3PriP1qsATVwIm68nbqqntOAr25NtGLptFHP6FcZQFjaLjxokU70PH8tAUiCpIOFVtJvg5JwhcWPUBcmIu2wsT00H2d0xxvcxoJkqM1jJ82tqqxbyME7.XyZeFDycLaNnu5PX1xcoxl51aozhyzr1y4s5541wWTWLTBBSTMv0leXxUp_ICQUXjeAi0JqKYQrAvpumATgcUv3DF36jqsc0EvoFvKasBc2q; Hm_lpvt_9004434f3a31031836fd6a07df4d1ae8=1602671016; 0yHmMQUi4f9KP=5UgMAOT5D0FGqqqmTxN_arA1AXmZIotzfCwQvytexgehCs90Qzy7_zAZat.lASXhqRIP9rLlsWsPsXRT7UE8CU_aknCuJen6JgnXxE0oSQw.FGQAyggsWK3UmoesUfnF0Hkt9ys4ULS9su7dlg_GdwlUIbjGlk9OpjFDW9GkKHM6shqofcYDscDQlFMUg2aT7UO2zIlrCrmj.vLVOOvS1Rdoy1apyvgH7nz0cKMYvlWyPS54v1rKhJoxz.Aom86kYV; JSESSIONID=T1QmovqPN9Y_l-W5j49PVn5qTwJslPSOMhLzNzblb4i_xyc-TUQF!-615896020");
        req.addHeader("Host","z.bj.189.cn");
        req.addHeader("Pragma","no-cache");
        req.addHeader("Referer","https://z.bj.189.cn/index/category.html");
        req.addHeader("Sec-Fetch-Dest","empty");
        req.addHeader("Sec-Fetch-Mode","cors");
        req.addHeader("Sec-Fetch-Site","same-origin");
        req.addHeader("X-Requested-With","XMLHttpRequest");
        req.addHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Mobile Safari/537.36");

        req.setUrl(url);
        Map<String, String> fields = new HashMap<>();
        //id=zb&dbcode=hgnd&wdcode=zb&m=getTree
        fields.put("busInfo","%7B%22shopId%22:%222620170428857852117203030016%22,%22sequenceNo%22:%225%22,%22page%22:%7B%7D,%22channelRel%22:%225%22,%22epps%22:%5B%5D,%22searchKey%22:%22%E7%89%B9%E6%83%A0%E8%B4%AD%E6%9C%BA%22%7D");
        fields.put("busInfoPara","%7B%22shopId%22:%222620170428857852117203030016%22,%22sequenceNo%22:%225%22,%22page%22:%7B%7D,%22channelRel%22:%225%22,%22epps%22:%5B%5D,%22searchKey%22:%22%E7%89%B9%E6%83%A0%E8%B4%AD%E6%9C%BA%22%7D");
        fields.put("sign","C15F529FEE267A8ED31696F8DDE50321");
        fields.put("appKey","C2F9BEBCE8DD3CEDFF8016A4B8248FF5");
        System.out.println( DateUtil.format(new Date(),PURE_DATETIME_PATTERN));
        fields.put("timeStamp", DateUtil.format(new Date(),PURE_DATETIME_PATTERN));
        req.setFields(fields);

        req.addParameter("PSAvnQfG","5oD5GMPFHTq0rv9NC1227CLuFEj2DcHy3fDJkbd6jYES4hP4aIIubuvc0ok4179A5f1C31pt.ldSHCh_mcRNQajWcqDZH.UBE7X.Ik_epkuenr839hSdSpUFRFyPz9T4FsmEQgyh2sJgN2ixmecO0bmxWN0i6W2fmusW5lCaJThNh4.BQvDL6k3gEV4AiqcdldJw2uR4JdzG9Zv9jE2bNpsstGD0.VYdb.LYwm_0uN8UGDwHbeodYMsvaJ47R0_6.v.VpDOQZ9W5h4J7TpKzYb_cTdPZ1bDqd9KxdAcM2LKE");

        Map<String, String> params = new HashMap<>();
        params.put("PSAvnQfG","5oD5GMPFHTq0rv9NC1227CLuFEj2DcHy3fDJkbd6jYES4hP4aIIubuvc0ok4179A5f1C31pt.ldSHCh_mcRNQajWcqDZH.UBE7X.Ik_epkuenr839hSdSpUFRFyPz9T4FsmEQgyh2sJgN2ixmecO0bmxWN0i6W2fmusW5lCaJThNh4.BQvDL6k3gEV4AiqcdldJw2uR4JdzG9Zv9jE2bNpsstGD0.VYdb.LYwm_0uN8UGDwHbeodYMsvaJ47R0_6.v.VpDOQZ9W5h4J7TpKzYb_cTdPZ1bDqd9KxdAcM2LKE");
/*
        GeccoEngine ge = GeccoEngine.create("com.agan.aganjavaspyder.crawler")
                .interval(5000)
                .loop(false)
                .seed(req)
                .engineStart();*/

        System.out.println(new BjHrefPipeline().setRequestParam(url, params));
    }



}
